package materialdesign.zjitc.net.materialdesign;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * 1.添加依赖
 * 2.在应用清单中，将application主题中AppCompat设置为NoActionBar
 * 3.定义主布局activity_mian.xml
 * 4.资源文件下创建menu包，创建mian.xml(Toolbar)
 * 5.创建Fragment对应的Activity类（Tablayout）
 * 6.定义适配器Adateper类
 * 7.定于MainActivity
 */
public class MainActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> mFragments;
    private List<String> mTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        mTabLayout=findViewById(R.id.tablayout);
        mToolbar=findViewById(R.id.toolbar);
        mViewPager=findViewById(R.id.viewpager);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.search:
                        Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.setting:
                        Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.share:
                        Toast.makeText(MainActivity.this, "分享", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });
        mTitles=new ArrayList<>();
        mTitles.add("头像1");
        mTitles.add("头像2");
        mTitles.add("头像3");
        mTitles.add("头像4");
        for (int i = 0; i <mTitles.size() ; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(mTitles.get(i)));
        }
        mFragments=new ArrayList<>();
        ListFragment listFragment=new ListFragment();
        ListFragment2 listFragment2=new ListFragment2();
        ListFragment3 listFragment3=new ListFragment3();
        ListFragment4 listFragment4=new ListFragment4();
        mFragments.add(listFragment);
        mFragments.add(listFragment2);
        mFragments.add(listFragment3);
        mFragments.add(listFragment4);
        mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(),mFragments,mTitles));
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(new FragmentAdapter(getSupportFragmentManager(),mFragments,mTitles));
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }


}
